package com.jintoufs.dao;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

/**
 * 通用DAO实现
 * 
 * @param <DAO>
 * @param <T>
 */
@Repository
public abstract class MyBatisSupport<DAO, T> extends SqlSessionDaoSupport {

	@Resource
	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		super.setSqlSessionFactory(sqlSessionFactory);
	}

	private DAO dao;

	private Class<DAO> mapperClass;

	@SuppressWarnings("unchecked")
	public MyBatisSupport() {
		Type[] types = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments();
		mapperClass = (Class<DAO>) types[0];
	}

	protected DAO getMapper() {
		dao = getSqlSession().getMapper(mapperClass);
		return dao;
	}

	protected String getMybatisNamespace() {
		return mapperClass.getName();
	}

}
